<?php
/**
 *UserLevelServices
 * @author tan bing
 * @date 2021-08-10 17:35
 */


namespace Modules\User\Services\Admin;


use Illuminate\Http\Request;
use Modules\User\Entities\UserLevel;
use Modules\User\Repositories\Contracts\UserLevelInterface;
use Modules\User\Validate\Admin\UserLevelValidate;

class UserLevelServices extends BaseService
{

    /**
     * @var Request
     * @author tan bing
     */
    private $request;

    /**
     * @var UserLevelInterface
     * @author tan bing
     */
    private $userLevelInterface;

    private $validate;

    /**
     * UserLevelServices constructor.
     *
     * @param Request $request
     * @param UserLevelInterface $userLevelInterface
     * @param UserLevelValidate $userLevelValidate
     * @author tan bing
     * @date 2021-08-10 18:05
     */
    public function __construct(Request $request, UserLevelInterface $userLevelInterface, UserLevelValidate $userLevelValidate)
    {
        $this->request = $request;
        $this->validate = $userLevelValidate;
        $this->userLevelInterface = $userLevelInterface;
    }

    /**
     * @return mixed
     * @author tan bing
     * @date 2021-08-10 17:49
     */
    public function getData()
    {
        $param = $this->request->input();
        $data = $this->userLevelInterface->getData($param);
        $count = count($data);
        return compact('count', 'data');
    }

    /**
     * @return mixed
     * @author tan bing
     * @date 2021-09-09 11:52
     */
    public function getUserLevel()
    {
        $param = $this->request->input();
        $data = $this->userLevelInterface->getUserLevel($param);
        return $data;
    }

    /**
     * @param null $id
     * @return mixed
     * @author tan bing
     * @date 2021-08-10 17:51
     */
    public function getDetail($id = null)
    {
        !$id && $id = $this->request->input('id', '');
        return $this->userLevelInterface->findById($id);
    }

    /**
     * @return mixed
     * @author tan bing
     * @date 2021-06-24 9:59
     */
    public function delete()
    {
        $id     = $this->request->get('id', '');
        $model = new UserLevel();
        return $model->forceDeleteData($id);
    }

    /**
     * @return mixed
     * @author tan bing
     * @date 2021-06-24 10:00
     */
    public function batchDelete()
    {
        $ids    = $this->request->get('ids', '');
        $model = new UserLevel();
        return $model->batchForceDeleteData($ids);
    }

    /**
     * @return mixed
     * @throws \Exception
     * @author tan bing
     * @date 2021-08-10 15:03
     */
    public function addAndEditDetail()
    {
        $data = $this->request->post();
        if (!$this->validate->scene('add')->check($data)) {
            throw new \Exception($this->validate->getError());
        }
        return $this->userLevelInterface->addAndEditDetail($data);
    }
}